﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TLCApp.pulic;


namespace TLCApp
{
    public partial class Rent11 : Form
    {
        public Rent11()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Rent11_Load(object sender, EventArgs e)
        {
            setcontrol();
            cmbtype.DataSource=Cls_Func.GetBaseData("C01");
            cmbtype.DisplayMember = "DataName";
            cmbtype.ValueMember = "DataType";
            querydata();
        }
        void setcontrol()
        {
            dgvlist.AutoGenerateColumns = false;
        }
        void querydata()
        {

            string strxml = "";

            strxml = "<DocumentElement><CMN_DEPT><CMN_DEPTCODE>" + filetrchar(txtBMDM.Text) + "</CMN_DEPTCODE>" ;
             strxml=strxml   +"<CMN_DEPT_MC> "+ filetrchar(this.txtBMMC.Text) + "</CMN_DEPT_MC>";
             strxml = strxml + "<ACTIVEFLAG> " + (this.ckbzt.Checked?"1":"0") + "</ACTIVEFLAG>" + "</CMN_DEPT></DocumentElement>";
             DataSet dsResult = Dal.sys.cmn_dept_query(strxml);
            dgvlist.DataSource = dsResult.Tables[0];
          
          
        }
        private string filetrchar( string strstr)
       {
           string reslut = strstr.Replace('<', ' ').Replace('>', ' ').Replace('/', ' ').Replace('\\', ' ');

            return reslut;
       }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            
            }
             DataTable dt = createTable("CMN_DEPT");
                DataRow dr = dt.NewRow();
                dr["CMN_DEPTCODE"] = txtBMDM.Text.Trim();
                dr["CMN_DEPT_MC"] = txtBMMC.Text.Trim();
                dr["CMN_DEPT_TYPE"] = cmbtype.SelectedValue;
                dr["CMN_DEPT_BZ"] = txtbz.Text.Trim();
                dr["ACTIVEFLAG"] = (chbflag.Checked ? "1" : "0");
                dr["PYM"] = pulic.Cls_Func.GetPyCode(txtBMMC.Text);
                dr["WBM"] = pulic.Cls_Func.GetWbm(txtBMMC.Text);
                dt.Rows.Add(dr);
                string strXml = pulic.XmlDataService.ToString(dt);
                DataSet dsResult = Dal.sys.cmn_dept_save(strXml,"0");
                querydata();
                clear();

                pulic.Cls_Popup.MsgBox("新增成功！");
        }
        DataTable createTable( string strTablename)
        {
            DataTable dt = new DataTable(strTablename);
            dt.Columns.Add("CMN_DEPTCODE");
            dt.Columns.Add("CMN_DEPT_MC");
            dt.Columns.Add("CMN_DEPT_TYPE");
            dt.Columns.Add("CMN_DEPT_BZ");
            dt.Columns.Add("ACTIVEFLAG");
            dt.Columns.Add("PYM");
            dt.Columns.Add("WBM");
            return dt;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            querydata();
        }

        private void dgvlist_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvlist.RowCount>0)
            {
                //txtBMDM.Text = dgvlist.CurrentRow.Cells["coldm"].Value.ToString();
                //txtBMMC.Text = dgvlist.CurrentRow.Cells["colmc"].Value.ToString();
                //txtbz.Text = dgvlist.CurrentRow.Cells["colbz"].Value.ToString();
                //cmbtype.SelectedValue = dgvlist.CurrentRow.Cells["collxdm"].Value.ToString();
                ////this.chbflag.Checked= dgvlist.CurrentRow.Cells["ACTIVEFLAG"].ToString()=='1'  
                //txtBMDM.ReadOnly = true;
                //btnAdd.Enabled = false;

            }
        }
        void clear()
        {
            txtBMDM.ReadOnly = false;
            txtBMDM.Text = "";
            txtBMMC.Text = "";
            txtbz.Text = "";
            cmbtype.SelectedValue = "1";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = false;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

             if (pulic.Cls_Popup.MsgYesNoBox("请确认是否修改！"))
            {

                if (txtBMDM.ReadOnly)
                {

                    if (!check())
                    {
                        return;

                    }
                    DataTable dt = createTable("CMN_DEPT");
                    DataRow dr = dt.NewRow();
                    dr["CMN_DEPTCODE"] = txtBMDM.Text.Trim();
                    dr["CMN_DEPT_MC"] = txtBMMC.Text.Trim();
                    dr["CMN_DEPT_TYPE"] = cmbtype.SelectedValue;
                    dr["CMN_DEPT_BZ"] = txtbz.Text.Trim();
                    dr["ACTIVEFLAG"] = (chbflag.Checked ? "1" : "0");
                    dr["PYM"] = pulic.Cls_Func.GetPyCode(txtBMMC.Text);
                    dr["WBM"] = pulic.Cls_Func.GetWbm(txtBMMC.Text);
                    dt.Rows.Add(dr);
                    string strXml = pulic.XmlDataService.ToString(dt);
                    DataSet dsResult = Dal.sys.cmn_dept_save(strXml, "1");
                    querydata();
                    clear();
                    pulic.Cls_Popup.MsgBox("修改成功！");
                }
            }
            else
            {
                pulic.Cls_Popup.MsgBox("请先新增！");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (pulic.Cls_Popup.MsgYesNoBox("请确认是否删除"))
            {

                if (txtBMDM.ReadOnly)
                {
                    DataTable dt = createTable("CMN_DEPT");
                    DataRow dr = dt.NewRow();
                    dr["CMN_DEPTCODE"] = txtBMDM.Text.Trim();
                    dr["CMN_DEPT_MC"] = txtBMMC.Text.Trim();
                    dr["CMN_DEPT_TYPE"] = cmbtype.SelectedValue;
                    dr["CMN_DEPT_BZ"] = txtbz.Text.Trim();
                    dr["ACTIVEFLAG"] = (chbflag.Checked ? "1" : "0");
                    dr["PYM"] = pulic.Cls_Func.GetPyCode(txtBMMC.Text);
                    dr["WBM"] = pulic.Cls_Func.GetWbm(txtBMMC.Text);
                    dt.Rows.Add(dr);
                    string strXml = pulic.XmlDataService.ToString(dt);
                    DataSet dsResult = Dal.sys.cmn_dept_save(strXml, "2");
                    if (dsResult.Tables[0].Rows[0]["rst"].ToString() == "T")
                    {
                        pulic.Cls_Popup.MsgBox("删除成功！");
                    }
                    else
                    {
                        pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
                    }
                    querydata();
                    clear();
                }
            }
            else
            {
                pulic.Cls_Popup.MsgBox("请选择删除！");
            }
        }

        private void dgvlist_Click(object sender, EventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {
                txtBMDM.Text = dgvlist.CurrentRow.Cells["coldm"].Value.ToString();
                txtBMMC.Text = dgvlist.CurrentRow.Cells["colmc"].Value.ToString();
                txtbz.Text = dgvlist.CurrentRow.Cells["colbz"].Value.ToString();
                cmbtype.SelectedValue = dgvlist.CurrentRow.Cells["collxdm"].Value.ToString();
                //this.chbflag.Checked= dgvlist.CurrentRow.Cells["ACTIVEFLAG"].ToString()=='1'  
                txtBMDM.ReadOnly = true;
                btnAdd.Enabled = false;
                btnUpdate.Enabled = true;

            }
        }

        bool check()
        {
            if (txtBMDM.Text.Trim().Length==0)
            {

                pulic.Cls_Popup.MsgBox("代码不能为空！");
                return false;
            }

            if ( this.txtBMMC .Text.Trim().Length == 0)
            {

                pulic.Cls_Popup.MsgBox("名称不能为空！");
                return false;
            }
            return true;
        }
    }
}
